package com.example.fish.controller;

import com.example.fish.pojo.User;
import com.example.fish.service.CollectionService;
import com.example.fish.utils.JsonResult;
import com.example.fish.vo.CollectionOwn;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping("/collection")
public class CollectionController extends BaseController{
    @Autowired
    private CollectionService service;

//    保存收藏
    @PostMapping("/save")
    private JsonResult<Void> save(Integer secretid, HttpSession session){
        User user = (User) session.getAttribute("user");
        service.collectSecret(secretid, user.getUserid());
        return new JsonResult<>(200,"success",null);
    }
    @PostMapping("/cancel")
    private JsonResult<Void> cancel(Integer secretid, HttpSession session){
        User user = (User) session.getAttribute("user");
        service.cancelCollect(secretid, user.getUserid());
        return new JsonResult<>(200,"success",null);
    }

    @PostMapping("/check")
    private JsonResult<Boolean> check(Integer secretid, HttpSession session){
        User user = (User) session.getAttribute("user");
        Boolean collected = service.isCollected(secretid, user.getUserid());
        return new JsonResult<>(200,"success",collected);
    }

    @GetMapping("/getOwn")
    private JsonResult<List<CollectionOwn>> getOwn(HttpSession session){
        User user =(User) session.getAttribute("user");
        List<CollectionOwn> list = service.findCollectionByUserid(user.getUserid());
        return new JsonResult<>(200,"success",list);
    }
    @PostMapping("/getOwnPage")
    private JsonResult<PageInfo<CollectionOwn>> getOwnPage(HttpSession session,Integer pageNum){
        User user =(User) session.getAttribute("user");
        PageInfo<CollectionOwn> pagein = service.findOwnByPage(pageNum, user.getUserid());
        return new JsonResult<>(200,"success",pagein);
    }

}
